import { createRouter, createWebHistory } from 'vue-router'
import cookie from 'js-cookie'
const Index = ()=>import('../views/Index.vue')  //主页
const Login = ()=>import('../views/Login.vue') //登录
const Error = ()=>import('../views/404.vue')  //404页面
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect:'/index'
    },
    {
      path: '/index',
      name: 'Index',
      redirect:"index/dashboard",
      component:Index,
      children:[
        {
          path:'redirect/:backpath(.*)',
          name:'redirect',
          meta:{
            title:''
          },
          component:()=>import('@/views/redirect/Index.vue') //页面刷新组件
        },
        {
          path:'userinfo',
          name:'userinfo',
          meta:{
            title:'personalinformation'
          },
          component:()=>import('@/views/admin/UserInfo.vue') //详细信息
        },
        {
          path:'changepassword',
          name:'changepassword',
          meta:{
            title:'changepassword'
          },
          component:()=>import('@/views/admin/ChangePassword.vue')  //修改密码
        }
      ]
    },
    {
      path:'/login',
      name:'Login',
      component:Login
    },
    {
      path:'/404',
      name:'Error',
      component:Error
    }
  ]
})

//路由拦截守卫
router.beforeEach((to,from)=>{
   //接收token
   const token = cookie.get('token');
   //判断是否有token和排除登录页面
   if(!token&&to.name!='Login'){
      router.replace({path:'/login',query:{back:to.fullPath}})
      return false
   }
   return true
})
export default router
